// export const api_url = 'http://192.168.1.151:7777/api';
export const api_url = 'https://teamwang.zenosh.com/api';
// export const img_url = 'http://192.168.1.151:7777';
export const img_url = 'https://teamwang.zenosh.com';

const request = (options = {}) => {
    let baseHeader = {
        "Content-Type": "application/json",
        "Accept": "application/json, text/plain, */*",
        "Authorization": 'Bearer ' + uni.getStorageSync('token'),
    };
    // uni.showLoading({
    //     title: '加载中...'
    // })
    return new Promise((resolve, reject) => {
        let baseUrl = /http/g.test(options.url) ? /http/g.test(options.url) : api_url;
        const parameter = {
            url: decodeURIComponent(baseUrl + options.url),
            method: options.type || 'GET',
            header: options.header || baseHeader,
            data: options.data || {}
        }
        uni.request(parameter).then(res => {
            // uni.hideLoading();
            // console.log(res, res?.data?.errcode);
            if (res?.data?.errcode || res.statusCode !== 200) {
                reject(res);
            } else if (res.statusCode == 200) {
                resolve(res.data);
            }
        }).catch(error => {
            console.log(error, '==失败==')
            // uni.hideLoading();
            reject(error)
        })

    })
}

const Fuc = (url, data, options = {}, Method) => {
    options.type = Method;
    options.data = data;
    options.url = url;
    return request(options)
}
const get = (url, data, options = {}) => Fuc(url, data, options = {}, 'GET')
const post = (url, data, options = {}) => Fuc(url, data, options, 'POST')
const put = (url, data, options = {}) => Fuc(url, data, options = {}, 'PUT')
const deletes = (url, data, options = {}) => Fuc(url, data, options = {}, 'DELETE')

export default {
    request,
    get,
    post,
    put,
    deletes
}
